//
//  HYGoodsCell.m
//  HaiYue
//
//  Created by skyline on 2018/6/19.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYGoodsCell.h"
@interface HYGoodsCell ()
@property (nonatomic, strong) HYView *imgBack;
@property (nonatomic, strong) HYImageView *imgView;
@property (nonatomic, strong) HYLabel *imgLBL;
@property (nonatomic, strong) HYLabel *jifenLBL;
@property (nonatomic, strong) HYLabel *vipLBL;
@property (nonatomic, strong) HYLabel *titleLBL;
@property (nonatomic, strong) HYLabel *contentLBL;
@end
@implementation HYGoodsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (HYView *)line
{
    if (!_line)
    {
        _line = [[HYView  alloc] initWithColor:CELL_LINE_COLOR];
        [self addSubview:_line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self);
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.height.mas_offset(GAP);
        }];
    }
    return _line;
}

- (HYView *)imgBack
{
    if (!_imgBack)
    {
        _imgBack = [[HYView alloc] initWithColor:[UIColor whiteColor]];
        _imgBack.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 2.0);
        [self addSubview:_imgBack];
        [_imgBack mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(GAP / 2.0);
            make.centerX.equalTo(self);
            make.size.mas_offset(self.imgBack.bounds.size);
        }];
    }
    return _imgBack;
}

- (HYImageView *)imgView
{
    if (!_imgView)
    {
        _imgView = [[HYImageView alloc] initWithImage:nil cornerRadius:0];
        _imgView.autoSize_imageURL = YES;
        [self.imgBack addSubview:_imgView];
        [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imgBack);
            make.centerX.equalTo(self.imgBack);
        }];
    }
    return _imgView;
}

- (HYLabel *)imgLBL
{
    if (!_imgLBL)
    {
        _imgLBL = [[HYLabel alloc] initWithText:@"" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:ContentFont]];
        [self addSubview:_imgLBL];
        [_imgLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imgView.mas_bottom).with.offset(GAP);
            make.left.equalTo(self).with.offset(GAP);
        }];
    }
    return _imgLBL;
}

- (HYLabel *)jifenLBL
{
    if (!_jifenLBL)
    {
        _jifenLBL = [[HYLabel alloc] initWithText:@"" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:ContentFont - 1 weight:UIFontWeightLight]];
        [self addSubview:_jifenLBL];
        [_jifenLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(GAP);
            make.top.equalTo(self.imgLBL.mas_bottom).with.offset(GAP);
        }];
    }
    return _jifenLBL;
}

- (HYLabel *)vipLBL
{
    if (!_vipLBL)
    {
        _vipLBL = [[HYLabel alloc] initWithText:@"" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:ContentFont - 1 weight:UIFontWeightLight]];
        [self addSubview:_vipLBL];
        [_vipLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imgLBL.mas_bottom).with.offset(GAP);
            make.left.equalTo(self.jifenLBL.mas_right).with.offset(GAP);
        }];
    }
    return _vipLBL;
}

- (HYLabel *)titleLBL
{
    if (!_titleLBL)
    {
        _titleLBL =  [[HYLabel alloc] initWithText:@"" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:ContentFont]];
        [self addSubview:_titleLBL];
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(GAP);
            make.left.equalTo(self).with.offset(GAP);
        }];
    }
    return _titleLBL;
}

- (HYLabel *)contentLBL
{
    if (!_contentLBL)
    {
        _contentLBL = [[HYLabel alloc] initWithText:@"" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:ContentFont - 1 weight:UIFontWeightLight]];
        _contentLBL.numberOfLines = 0;
        _contentLBL.fixedWidth = SCREEN_WIDTH - GAP * 2.0;
        [self addSubview:_contentLBL];
        [_contentLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLBL.mas_bottom).with.offset(GAP);
            make.left.equalTo(self).with.offset(GAP);
        }];
    }
    return _contentLBL;
}

- (void)setType:(HYGoodsCellType)type
{
    if (_type != type)
    {
        _type = type;
        self.imgBack.hidden = (_type == HYGoodsCellTypeImage)?NO:YES;
        self.imgView.hidden = (_type == HYGoodsCellTypeImage)?NO:YES;
        self.imgLBL.hidden = (_type == HYGoodsCellTypeImage)?NO:YES;
        self.jifenLBL.hidden = (_type == HYGoodsCellTypeImage)?NO:YES;
        self.vipLBL.hidden = (_type == HYGoodsCellTypeImage)?NO:YES;
        self.titleLBL.hidden = (_type == HYGoodsCellTypeText)?NO:YES;
        self.contentLBL.hidden = (_type == HYGoodsCellTypeText)?NO:YES;
    }
}

- (void)setImgURL:(NSString *)imgURL
{
    if (_imgURL != imgURL)
    {
        _imgURL = imgURL;
        self.imgView.imageURL = _imgURL;
    }
}

- (void)setImgTitle:(NSString *)imgTitle
{
    if (_imgTitle != imgTitle)
    {
        _imgTitle = imgTitle;
        self.imgLBL.text = _imgTitle;
    }
}

- (void)setJifen:(NSString *)jifen
{
    if (_jifen != jifen)
    {
        _jifen = jifen;
        self.jifenLBL.attributedText = [self jifen:_jifen];
    }
}

- (void)setVip:(NSString *)vip
{
    if (_vip != vip)
    {
        _vip = vip;
        self.vipLBL.attributedText = [self vip:_vip];
    }
}

- (void)setTitle:(NSString *)title
{
    if (_title != title)
    {
        _title = title;
        self.titleLBL.text = _title;
    }
}

- (void)setContent:(NSString *)content
{
    if (_content != content)
    {
        _content = content;
        self.contentLBL.text = _content;
    }
}

- (NSAttributedString *)jifen:(NSString *)jifen
{
    NSMutableAttributedString *attrJifen = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"兑换积分:%@",jifen]];
    [attrJifen addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:ContentFont - 1 weight:UIFontWeightLight] range:NSMakeRange(0, attrJifen.length)];
    [attrJifen addAttribute:NSForegroundColorAttributeName value:TEXT_BLACK_COLOR range:NSMakeRange(0, 5)];
    [attrJifen addAttribute:NSForegroundColorAttributeName value:RED_COLOR range:NSMakeRange(5, attrJifen.length - 5)];
    return attrJifen;
}

- (NSAttributedString *)vip:(NSString *)vip
{
    NSMutableAttributedString *attrVIP = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"兑换等级:VIP%@",vip]];
    [attrVIP addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:ContentFont - 1 weight:UIFontWeightLight] range:NSMakeRange(0, attrVIP.length)];
    [attrVIP addAttribute:NSForegroundColorAttributeName value:TEXT_BLACK_COLOR range:NSMakeRange(0, 5)];
    [attrVIP addAttribute:NSForegroundColorAttributeName value:RED_COLOR range:NSMakeRange(5, attrVIP.length - 5)];
    return attrVIP;
}

- (CGFloat)height
{
    if (self.type == HYGoodsCellTypeImage)
    {
        return self.imgBack.bounds.size.height + self.imgLBL.bounds.size.height + self.jifenLBL.bounds.size.height + GAP * 4.5;
    }
    else
    {
        return self.titleLBL.bounds.size.height + self.contentLBL.bounds.size.height + GAP * 4;
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
